package com.txh.blog.controller;

import com.txh.blog.consts.Consts;
import com.txh.blog.model.UserInfo;
import com.txh.blog.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 界面之间的跳转
 */
@Controller
public class BaseController {


    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("/back")
    public String login(){
        return "admin/login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(UserInfo userInfo, HttpSession session, HttpServletResponse response, Model model){
        boolean flag = userInfoService.findUser(userInfo);
        if (flag){
            session.setAttribute(Consts.SESSION_USER,userInfo);
            return "redirect:/admin/index";
        }else{
            model.addAttribute(Consts.ERROR_INFO,"用户名或密码错误");
            return "admin/login";
        }

    }

    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:/back";
    }
}
